package com.vtquang.mymaps.overlay;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;
import com.vtquang.mymaps.R;

public class Direction_Overlay extends MyOverlay {

	List<GeoPoint> _lstPath;
	private int _color;
	private Context _context; 
	
	public Direction_Overlay(List<GeoPoint> lstPath, Context context) {
		this._lstPath = lstPath;
		this._context = context;
		this._color = 0x5F61B7FF;// rgb(74, 229, 252);//128,
													// 136, 231);
	}

	public Direction_Overlay(List<GeoPoint> lstPath, int color) {
		this._lstPath = lstPath;
		this._color = color;
	}
	  
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) { 
		Projection projection = mapView.getProjection();
		 
		Paint paint = new Paint();
		paint.setStrokeWidth(5);
		paint.setStyle(Paint.Style.STROKE);

		paint.setDither(false);

		paint.setAntiAlias(false);
		paint.setAlpha(120);
		paint.setColor(this._color);

		Point pSrc = new Point();
		Point pDes = new Point();
		Path path = new Path();

		int count = _lstPath.size();
		if (count > 0) {
			projection.toPixels(_lstPath.get(0), pSrc);
			path.moveTo(pSrc.x, pSrc.y);
			for (int i = 1; i < count; i++) {
				projection.toPixels(_lstPath.get(i), pDes);
				path.lineTo(pDes.x, pDes.y);
			}
			
			Resources res = _context.getResources(); 
			Bitmap _ic_src = BitmapFactory.decodeResource(res, R.drawable.src);
			Bitmap _ic_des = BitmapFactory.decodeResource(res, R.drawable.des);
			   
			canvas.drawPath(path, paint); 
			drawLocation(_ic_src, pSrc, canvas);
			drawLocation(_ic_des, pDes, canvas);
		}
   
		return super.draw(canvas, mapView, shadow, when);
	}
	
	private void drawLocation(Bitmap bmp, Point p, Canvas canvas){
		int left = (int) (p.x - bmp.getWidth() / 2);
		int top = (int) (p.y - bmp.getHeight());
 
		canvas.drawBitmap(bmp, left, top, null); 
	}
	
	public static String getNameOverlay(){
		return "DirectionOverlay";
	}
	@Override
	public String getName() {
		 return getNameOverlay();
	}
}
